﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：TableIndexContextMenu.cs
           // 文件功能描述：具体表索引的右键菜单。
           //
           // 
           // 创建标识：杨波20080924
           //
           // 修改标识：
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/
using System.Windows.Forms;
using System.Data;
namespace RuleCode.ObjectTree
{
    public partial class TableIndexContextMenu : NewAndDeleteContextMenu
    {
        public TableIndexContextMenu()
        {
            InitializeComponent();
            newToolStripMenuItem.Text = "新建索引字段";
            custInit();
            baseContextMenuStrip.Items.Insert(4, indexSycToolStripMenuItem);

            }
        private System.Windows.Forms.ToolStripMenuItem indexSycToolStripMenuItem;
        private void custInit()
        {
            this.indexSycToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            // 
            // 同步索引ToolStripMenuItem
            // 
            this.indexSycToolStripMenuItem.Name = "indexSycToolStripMenuItem";
            this.indexSycToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.indexSycToolStripMenuItem.Text = "同步索引";
            this.indexSycToolStripMenuItem.Click += new System.EventHandler(this.indexSycToolStripMenuItem_Click);
        }
        protected override void addNewNode()
        {
            base.addNewNode();
            string name = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.Index, OT.IndexField,
                                       typeof(TableIndexFieldProperty), "");
        }

        private void indexSycToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            string tableName = Util.SelectedNode.SysXmlNode.ParentNode.ParentNode.Name;
            string indexName = Util.SelectedNode.Text;
           SysTreeNode indexNode = Util.SelectedNode;
            if (indexNode.Nodes.Count < 1)
            {
                MessageBox.Show("索引字段为空");
            }
            else 
            {
                int count = indexNode.Nodes.Count;
                string[] cols = new string[count];
                for (int i = 0; i < count; i++)
                {
                    indexNode.ExpandAllNodes();
                    Util.SetProerptyInSilent(((SysTreeNode)indexNode.Nodes[i]));

                    cols[i] = ((TableIndexFieldProperty)((SysTreeNode)indexNode.Nodes[i]).Property).DataField;
                }
                string ss = string.Join(",", cols);
                if (!string.IsNullOrEmpty(ss))
                {
                    //if existe,alter,else create

                    SqlHelper sqlHelp = SqlHelper.Instance;

                    //CREATE UNIQUE CLUSTERED INDEX Idx1 ON t1(c);
                    //                   IF EXISTS(SELECT ObjKey FROM tCarsPicture WHERE ObjKey=@ObjKey AND PicturePostion = @PicturePostion)
                    // BEGIN
                    //   UPDATE tCarsPicture SET PictureData = @PictureData,UpdateTime=GETDATE()
                    //  WHERE ObjKey=@ObjKey AND PicturePostion = @PicturePostion;
                    //   SET @O_RETURN = @@ERROR;
                    // END
                    //ELSE 
                    //  INSERT INTO tCarsPicture(ObjKey,PictureData,PicturePostion,UpdateTime) 
                    //  VALUES(@ObjKey,@PictureData,@PicturePostion,GETDATE());
                    // SET @O_RETURN = @@ERROR;
                    //                  if   exists(select  1  from  sysindexes  where  name='Index_test_vcMemo')  
                    //begin  
                    //  CREATE  INDEX [Index_test_vcMemo] ON [dbo].[test]([vcMemo]) ON [PRIMARY]  
                    //end

                    string sql = "if  exists (select * from sysindexes a join sysobjects b on a.id=b.id and b.name='" + tableName + "' and b.type='u' where a.name='" + indexName + "')";
                    sql += " Begin\n DROP INDEX " + indexName +
                                    " ON [" + tableName + "];\n"; sql += "CREATE INDEX " + indexName +
                                         " ON [" + tableName + "](" + ss + ");\n ";
                    sql += "End\n";
                    sql += "else\n";
                    sql += "CREATE INDEX " + indexName +
                            " ON [" + tableName + "](" + ss + ");\n ";
                    sqlHelp.ExecuteNonQuery(CommandType.Text,
                                            sql);

                }
                else
                {
                    MessageBox.Show("请指定索引字段DataField");
                }
                
               
            }


        }
        protected override void ExecSql()
        {
            string tableName = Util.SelectedNode.SysXmlNode.ParentNode.ParentNode.Name;
            string indexName = Util.SelectedNode.Text;
            base.ExecSql();
            SqlHelper sqlHelp = SqlHelper.Instance;
            sqlHelp.ExecuteNonQuery(CommandType.Text,
                                    "DROP INDEX " + indexName +
                                    "ON " + tableName + ";");
    //        DROP INDEX IX_ProductVendor_VendorID 
    //ON Purchasing.ProductVendor;


        }
}
}